package tushuguanlixitong.text;  

import org.openqa.selenium.By;  
import org.openqa.selenium.JavascriptExecutor;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.WebElement;  
import org.openqa.selenium.chrome.ChromeDriver;  
import org.openqa.selenium.support.ui.ExpectedConditions;  
import org.openqa.selenium.support.ui.WebDriverWait;  
import java.time.Duration;  

public class LogintextTest {  
    public static void main(String[] args) {  
        // 设置ChromeDriver的路径  
        System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe"); // 确保exe后缀  

        // 创建WebDriver实例  
        WebDriver driver = new ChromeDriver();  

        try {  
            // 打开登录页面  
            driver.get("http://localhost:8080/tushuguanlixitong/adminLogin.html");  

            // 使用 WebDriverWait 等待用户名输入框可见  
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));  
            WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("username")));  
            WebElement passwordInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("password")));  
            WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type='submit']")));  

            // 模拟输入  
            usernameInput.sendKeys("admin"); // 有效的用户名  
            passwordInput.sendKeys("admin"); // 有效的密码  

            // 点击登录按钮  
            loginButton.click();  

            // 等待成功消息  
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("successMessage"))); // 根据实际情况修改元素的 ID  

            // 获取成功提示消息并输出  
            WebElement successMessage = driver.findElement(By.id("successMessage"));  
            System.out.println("Success Message: " + successMessage.getText());  

        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            // 关闭浏览器  
            driver.quit();  
        }  
    }  
}